<?php

namespace App\Http\Controllers\Api;

use App\Models\Product;
use App\Models\Company;
use Illuminate\Http\Request;
use App\Http\Requests\Api\ProductRequest;
use App\Transformers\ProductTransformer;

class ProductsController extends Controller
{
    public function search(Request $request, Product $product)
    {
        $per_page = $request->input('per_page', 20);
        $query = $product->query();
        $query->where('delflag', false);

        if ($name = $request->name) {
            $query=$query->where('name', 'like', "%{$request->name}%");
        }

        if ($price = $request->price) {
            $query=$query->where('price', '>=', $request->price);
        }

        if ($company_id = $request->company_id) {
            $query=$query->where('company_id', $request->company_id);
        }


        if ($name = $request->type_id) {
            $query=$query->whereHas('types', function($q) use ($request)
                {
                    $q->where('id',  $request->type_id);
                });
        }

        if ($orderBy = $request->orderBy) {
            $query=$query->ordered();
        } else {
            $query=$query->recent();
        }

        $products = $query->paginate($per_page);

        return $this->response->paginator($products, new ProductTransformer());
    }

    public function index(Request $request, Product $product)
    {
        $per_page = $request->input('per_page', 20);
        $query = $product->query();


        $query->where('delflag', false);

        if ($orderBy = $request->orderBy) {
            $query=$query->ordered();
        } else {
            $query=$query->recent();
        }

        $products = $query->paginate($per_page);

        return $this->response->paginator($products, new ProductTransformer());
    }

    public function companyIndex(Request $request, Company $company)
    {
        $per_page = $request->input('per_page', 20);

        $products=null;

        if ($orderBy = $request->orderBy) {
            $products = $company->products()->where('delflag', false)->ordered()
                    ->paginate($per_page);
        } else {
            $products = $company->products()->where('delflag', false)->recent()
                    ->paginate($per_page);
        }

        return $this->response->paginator($products, new ProductTransformer());
    }

    public function show(Company $company, Product $product)
    {
        return $this->response->item($product, new ProductTransformer());
    }

	public function store(ProductRequest $request, Company $company, Product $product)
	{
        $this->authorize('update', $product);
		$product->fill($request->all());
        $company->products()->save($product);

        return $this->response->item($product, new ProductTransformer())
            ->setStatusCode(201);
	}

	public function update(ProductRequest $request, Company $company, Product $product)
    {
        $this->authorize('update', $product);
		$product->update($request->all());

		return $this->response->item($product, new ProductTransformer())
            ->setStatusCode(201);
	}

	public function destroy(Company $company, Product $product)
	{
        $this->authorize('destroy', $product);
		$product->delflag=true;
        $product->save();

        return $this->response->array([
            'status'=>true,
            'data'=>[
                'successflag'=>true,
            ],
            'message'=>'删除成功',
        ])->setStatusCode(200);
    }

    public function attachCompany(Request $request, Product $product)
    {
        $this->authorize('update', $product);
        $product->company_id=$request->company_id;
        $product->save();

        return $this->response->item($product, new ProductTransformer())
            ->setStatusCode(201);
    }

    public function addType(Request $request, Product $product)
    {
        $this->authorize('update', $product);
        $product->types()->sync($request->type_ids);
        return $this->response->array([
            'status'=>true,
            'data'=>[
                'successflag'=>true,
            ],
            'message'=>'类型修改成功',
        ])->setStatusCode(200);
    }

}